//
//  DiaryCell.m
//  MyTrip
//
//  Created by zhang on 15/8/23.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "DiaryCell.h"
#import "Diary.h"
#import "UIImageView+WebCache.h"
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

@implementation DiaryCell

- (void)dealloc
{
    [_greenLabel release];
    [_addLabel release];
    [_dateLabel release];
    [_authorLabel release];
    [_authorImage release];
    [_titleLabel release];
    [_picImage release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.picImage = [[[UIImageView alloc] initWithFrame:CGRectMake(5 * OffWidth, 5 * OffHeight, ScreenWidth - 10 * OffWidth, (200 - 10)  * OffHeight)] autorelease];
        [self.contentView addSubview:_picImage];
        _picImage.layer.cornerRadius = 5;
        _picImage.layer.masksToBounds = YES;
        
        self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15 * OffWidth, 5 * OffHeight, ScreenWidth - 20 * OffWidth, 40 * OffHeight)] autorelease];
        [self.contentView addSubview:_titleLabel];
        _titleLabel.shadowOffset = CGSizeMake(2 * OffWidth, 2 * OffHeight);
        _titleLabel.shadowColor = [UIColor blackColor];
        _titleLabel.alpha = 0.5;
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        _titleLabel.numberOfLines = 0;
        
        self.greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15 * OffWidth, _titleLabel.frame.size.height + _titleLabel.frame.origin.y + 5 * OffHeight, 4 * OffWidth, (30 - 3) * OffHeight)] autorelease];
        [self.contentView addSubview:_greenLabel];
        
        self.dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(_greenLabel.frame.origin.x + _greenLabel.frame.size.width + 4 * OffWidth, _titleLabel.frame.size.height + _titleLabel.frame.origin.y, ScreenWidth - 20 * OffWidth, 15 * OffHeight)] autorelease];
        [self.contentView addSubview:_dateLabel];
        _dateLabel.shadowOffset = CGSizeMake(2, 2);
        _dateLabel.shadowColor = [UIColor blackColor];
        _dateLabel.alpha = 0.5;
        
        self.addLabel = [[[UILabel alloc] initWithFrame:CGRectMake(_greenLabel.frame.origin.x + _greenLabel.frame.size.width + 4 * OffWidth, _dateLabel.frame.size.height + _dateLabel.frame.origin.y, ScreenWidth - 10 * OffWidth, 15 * OffHeight)] autorelease];
        [self.contentView addSubview:_addLabel];
        _addLabel.shadowOffset = CGSizeMake(2, 2);
        _addLabel.shadowColor = [UIColor blackColor];
        _addLabel.alpha = 0.5;
        
        self.authorImage = [[[UIImageView alloc] initWithFrame:CGRectMake(15 * OffWidth, _picImage.frame.size.height + _picImage.frame.origin.y - 50 * OffHeight, 30 * OffWidth, 30 * OffHeight)] autorelease];
        _authorImage.layer.cornerRadius = 15;
        _authorImage.layer.masksToBounds = YES;
        [self.contentView addSubview:_authorImage];
        
        self.authorLabel = [[[UILabel alloc] initWithFrame:CGRectMake(_authorImage.frame.size.width + _authorImage.frame.origin.x + 5 * OffWidth, _picImage.frame.size.height + _picImage.frame.origin.y - 50 *OffHeight, 100 * OffWidth, 30 * OffHeight)] autorelease];
        [self.contentView addSubview:_authorLabel];
        
    }
    return self;
}

- (void)setValueWithDiary:(Diary *)diary{
    [_picImage sd_setImageWithURL:[NSURL URLWithString:diary.pic] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    [_titleLabel setText:diary.title];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont systemFontOfSize:20];
    [_greenLabel setBackgroundColor:[UIColor greenColor]];
    
    NSString *date = [diary.date stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    NSString *dateText = [NSString stringWithFormat:@"%@   %@天    %@次浏览", date, diary.dayCount, diary.viewCount];
    [_dateLabel setText:dateText];
    _dateLabel.textColor = [UIColor whiteColor];
    _dateLabel.font = [UIFont systemFontOfSize:10];
    
    
    [_addLabel setText:diary.address];
    _addLabel.textColor = [UIColor whiteColor];
    [_addLabel setFont:[UIFont systemFontOfSize:10]];
    
    [_authorImage sd_setImageWithURL:[NSURL URLWithString:diary.authorImage] placeholderImage:nil];
    
    NSString *name = [NSString stringWithFormat:@"by%@", diary.authorName];
    [_authorLabel setText:name];
    _authorLabel.font = [UIFont systemFontOfSize:12];
    
    _authorLabel.textColor = [UIColor whiteColor];
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
